package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.database.ProductBean
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.widget.DividerItemDecoration


class OrderInfoDialog(context: Context) : Dialog(context, R.style.ChooseDialog) {

    private var mOrderRcy: RecyclerView? = null
    private var mOrderAdapter: OrderAdapter? = null
    private var mOnGuaDanDialogListener: OrderInfoDialogListener? = null
    private var dialog_tv1: TextView? = null
    private var dialog_tv2: TextView? = null
    private var dialog_tv3: TextView? = null
    private var dialog_tv4: TextView? = null
    private var dialog_tv5: TextView? = null
//    private var mOrderBean: OrderBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_order_info)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }
        mOrderRcy = findViewById(R.id.mOrderRcy)
        dialog_tv1 = findViewById(R.id.dialog_tv1)
        dialog_tv2 = findViewById(R.id.dialog_tv2)
        dialog_tv3 = findViewById(R.id.dialog_tv3)
        dialog_tv4 = findViewById(R.id.dialog_tv4)
        dialog_tv5 = findViewById(R.id.dialog_tv5)
        initOrderRcy()

        findViewById<View>(R.id.mCloseIv).apply {
            setOnClickListener {
                dismiss()
            }
        }
    }

    private fun initOrderRcy() {
        (mOrderRcy?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mOrderRcy?.addItemDecoration(
            DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL_LIST, 1, ContextCompat.getColor(App.context!!, R.color.line_color), true)
        )
        mOrderAdapter = OrderAdapter()
        mOrderRcy?.layoutManager = LinearLayoutManager(context)
        mOrderRcy?.adapter = mOrderAdapter
        mOrderAdapter?.bindToRecyclerView(mOrderRcy)
        mOrderAdapter?.setEmptyView(R.layout.layout_status_layout_manager_empty1)
    }

    @SuppressLint("CheckResult")
//    private fun updateOrderInfo(orderBean: OrderBean?) {
////        dialog_tv1?.text = orderBean?.order_sn ?: "暂无"
////        dialog_tv2?.text = orderBean?.add_time_text ?: "暂无"
//        dialog_tv3?.text = "￥".plus(orderBean?.total_price ?: "0.00")
//        dialog_tv5?.let {
//            RxView.clicks(it)
//                .throttleFirst(1, TimeUnit.SECONDS)
//                .subscribe {
//                    mOnGuaDanDialogListener?.onDelOrder(orderBean)
//                }
//        }
//        dialog_tv4?.let {
//            RxView.clicks(it)
//                .throttleFirst(3, TimeUnit.SECONDS)
//                .subscribe {
//                    mOrderBean?.let { ob ->
//                        EventBusUtil.sendEvent(Event(EventCode.PRINT_ORDER, ob))
//                    }
//                }
//        }
//        CommUtils.println("GuaDanDialog:", "${orderBean?.goodsList?.size ?: 0}")
//        mOrderAdapter?.setNewData(orderBean?.goodsList)
//    }

    inner class OrderAdapter : BaseQuickAdapter<ProductBean, BaseViewHolder>(R.layout.item_dialog_order, null) {
        override fun convert(helper: BaseViewHolder, item: ProductBean) {
            helper
                .setText(R.id.item_tv1, item.product_name)
                .setText(R.id.item_tv2, item.sale_price)
                .setText(R.id.item_tv3, item.weight)
                .setText(R.id.item_tv4, item.price_sub_total)
        }
    }

    fun setOnGuaDanDialogListener(l: OrderInfoDialogListener) {
        mOnGuaDanDialogListener = l
    }

//    fun onShowData(orderInfo: OrderBean) {
//        show()
////        mOrderBean = orderInfo
////        updateOrderInfo(mOrderBean)
//    }

    interface OrderInfoDialogListener {
//        fun onDelOrder(orderBean: OrderBean?)
    }
}
